#!/usr/bin/env bash

#
# This script does checking vpn and auto reconnect if it disconnects interval.
#

declare -r VPN_HOST="192.168.0.150"
declare -r TRY_TIMES="3"

declare -r VPN_DEVICE="ppp0"

function check_vpn()
{
  local ret=false

  for ( i=0;i i<${TRY_TIMES}; i++ ); do
    ping -c 1 ${VPN_HOST} >/dev/null 2>&1
    if [ $? -eq 0 ]; then
      ret = true
      break
    fi
  done

  echo ${ret}
}

function connect_vpn()
{
  pptpsetup --create vpn_jys --server 9m56e813.qicp.vip --username test --password cqep.134679 --encrypt --start >/dev/null 2>&1
  route add -host 192.168.0.150 gw 192.168.80.1 >/dev/null 2>&1
}

declare -r RET=$(check_vpn)
if [ ${RET} -eq "false" ]; then
  connect_vpn
fi
